﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Safe_Driving.EasyCalendar.Controler
{
    /// <summary>
    ///
    ///     <MyNamespace:EventPanel/>
    ///
    /// </summary>
    public class EventPanel : Panel
    {
        protected override Size MeasureOverride(Size availableSize)
        {
            // Calculate size based on duration?
            Size size = new Size(double.PositiveInfinity, double.PositiveInfinity);

            foreach (UIElement element in this.Children)
            {
                element.Measure(size);
            }

            return new Size();
        }

        public static double getX_offset(DateTime day)
        {
            return (double)((getDayint(day)) * 130);
        }

        public static int getDayint(DateTime d)
        {
            int i;

            if ((int)d.DayOfWeek == 0)
            {
                i = 6;
            }
            else
            {
                i = (int)d.DayOfWeek - 1;
            }

            return i;
        }


        protected override Size ArrangeOverride(Size finalSize)
        {
            double header_offset = 0;


            double top = 0;
            double left = 0;
            double width = 130;
            double height = 0;

            foreach (UIElement element in this.Children)
            {
                Nullable<DateTime> startTime = element.GetValue(EventCalendar.StartTimeProperty) as Nullable<DateTime>;
                Nullable<DateTime> endTime = element.GetValue(EventCalendar.EndTimeProperty) as Nullable<DateTime>;

                double start_minutes = (startTime.Value.Hour * 60) - (7 * 60);
                double end_minutes = (endTime.Value.Hour * 60) - (7 * 60);
                double start_offset = ((finalSize.Height - header_offset) / (15 * 60)) * start_minutes + header_offset;
                double end_offset = ((finalSize.Height - header_offset) / (15 * 60)) * end_minutes + header_offset;

                top = start_offset + 7;

                height = (end_offset - start_offset) - 2;

                left = getX_offset(startTime.Value);

                element.Arrange(new Rect(left, top, width, height));
                //element.Arrange(new Rect(left, header_offset, 130, 50));

            }

            return finalSize;
        }


    }
}
